#--coding:utf-8--
# @Time    : 2022/5/10 20:44
# @Author  : hakold
# @File    : equip_model.py
# @PROJECT : chatroom_game

import json
from model import player_model
import pymongo

global equip_config
equip_config = json.load(open("config/equip_config.json", encoding='utf-8'))

def mongo_connect():
    #myclient = pymongo.MongoClient('mongodb+srv://hakold:52Beebee@cluster0.gy1rb.mongodb.net/xiuxian?retryWrites=true&w=majority/')
    myclient = pymongo.MongoClient('mongodb://127.0.0.1:27017/')
    #dblist = myclient.list_database_names()
    # dblist = myclient.database_names() 
    mydb = myclient["qt_game"] #创建数据库
    #collist = mydb.list_collection_names()
    mycol = mydb["player"] #集合
    return mycol

def check(data_dict):  # 获取装备信息
    # inner_type = data_dict["inner_type"].strip()
    data = {}
    account = data_dict['account']
    mycol = mongo_connect()
    check_query = { "account" : account }
    check_doc = mycol.find(check_query)
    check_list = []
    for x in check_doc:
        check_list.append(x)
    player_data = check_list[0]
    del player_data["_id"]
    code = "200"
    data["account"] = player_data["account"]
    data["type"] = "equip_get"
    data["code"] = code
    data["data"] = player_data
    data = json.dumps(data)
    return data

def equip_worn(self, data_dict):  
    # 穿戴装备
    # inner_type = data_dict["inner_type"].strip()
    data = {}
    account = data_dict['account']
    mycol = mongo_connect()
    check_query = { "account" : account }
    check_doc = mycol.find(check_query)
    check_list = []
    for x in check_doc:
        check_list.append(x)
    player_data = check_list[0]
    del player_data["_id"]

    # now_worn = check_now_equip_status(player_data) # 获得当前装备的装备列表
    # equip_change_attr_reduce(self, account, now_worn) # 减掉他们的属性

    if data_dict.get('equiptype1'):
        equiptype1 = data_dict['equiptype1'] # 玩家选择的装备
        equiptype1_list = player_data['equip']['1']
        result = check_equip_status(equiptype1_list)
        if result[0]:
            # 判断玩家装备列表是否为空
            # 不为空就是有装备
            # 返回一个全部卸下的列表
            equiptype1_list = result[1] # 这里是全部没穿戴装备的list
            new_equiptype1_list = equip_list_worn(equiptype1_list, equiptype1)
            player_data['equip']['1'] = new_equiptype1_list

    if data_dict.get('equiptype2'):
        equiptype2 = data_dict['equiptype2']
        equiptype2_list = player_data['equip']['2']
        result = check_equip_status(equiptype2_list)
        if result[0]:
            # 判断玩家装备列表是否为空
            # 不为空就是有装备
            # 返回一个全部卸下的列表
            equiptype2_list = result[1] # 这里是全部没穿戴装备的list
            new_equiptype2_list = equip_list_worn(equiptype2_list, equiptype2)
            player_data['equip']['2'] = new_equiptype2_list

    if data_dict.get('equiptype3'):
        equiptype3 = data_dict['equiptype3']
        equiptype3_list = player_data['equip']['3']
        result = check_equip_status(equiptype3_list)
        if result[0]:
            # 判断玩家装备列表是否为空
            # 不为空就是有装备
            # 返回一个全部卸下的列表
            equiptype3_list = result[1] # 这里是全部没穿戴装备的list
            new_equiptype3_list = equip_list_worn(equiptype3_list, equiptype3)
            player_data['equip']['3'] = new_equiptype3_list

    if data_dict.get('equiptype4'):
        equiptype4 = data_dict['equiptype4']
        equiptype4_list = player_data['equip']['4']
        result = check_equip_status(equiptype4_list)
        if result[0]:
            # 判断玩家装备列表是否为空
            # 不为空就是有装备
            # 返回一个全部卸下的列表
            equiptype4_list = result[1] # 这里是全部没穿戴装备的list
            new_equiptype4_list = equip_list_worn(equiptype4_list, equiptype4)
            player_data['equip']['4'] = new_equiptype4_list

    if data_dict.get('equiptype5'):
        equiptype5 = data_dict['equiptype5']
        equiptype5_list = player_data['equip']['5']
        result = check_equip_status(equiptype5_list)
        if result[0]:
            # 判断玩家装备列表是否为空
            # 不为空就是有装备
            # 返回一个全部卸下的列表
            equiptype5_list = result[1] # 这里是全部没穿戴装备的list
            new_equiptype5_list = equip_list_worn(equiptype5_list, equiptype5)
            player_data['equip']['5'] = new_equiptype5_list

    if data_dict.get('equiptype6'):
        equiptype6 = data_dict['equiptype6']
        equiptype6_list = player_data['equip']['6']
        result = check_equip_status(equiptype6_list)
        if result[0]:
            # 判断玩家装备列表是否为空
            # 不为空就是有装备
            # 返回一个全部卸下的列表
            equiptype6_list = result[1] # 这里是全部没穿戴装备的list
            new_equiptype6_list = equip_list_worn(equiptype6_list, equiptype6)
            player_data['equip']['6'] = new_equiptype6_list
    
    # 保存穿上的装备
    newvalues = { "$set": { 'equip' : player_data['equip'] } }
    mycol.update_one({ "account" : player_data['account'] }, newvalues)

    # 增加穿上装备的属性
    # now_worn = check_now_equip_status(player_data) # 获得当前装备的装备列表
    # equip_change_attr_up(self, player_data['account'], now_worn)

    code = "200"
    data["account"] = player_data["account"]
    data["type"] = "equip_worn"
    data["code"] = code
    data["data"] = player_data
    data = json.dumps(data)
    return data
    
def check_equip_status(type_list):
    # 某类型的装备list列表
    # 检查列表长度
    if len(type_list) == 0:
        # 玩家没有这类装备
        return [False]
    else:
        # 玩家有这类装备
        # 把装备全脱了
        for equip_index in range(len(type_list)):
            type_list[equip_index]['status'] = 0

        return [True, type_list]

def check_now_equip_status(player_data):
    # 检查所有穿戴装备
    # 返回一个dict
    now_worn = {}
    for equip in player_data['equip']['1']:
        if equip['status'] == 1:
            now_worn['1'] = equip

    for equip in player_data['equip']['2']:
        if equip['status'] == 1:
            now_worn['2'] = equip

    for equip in player_data['equip']['3']:
        if equip['status'] == 1:
            now_worn['3'] = equip

    for equip in player_data['equip']['4']:
        if equip['status'] == 1:
            now_worn['4'] = equip

    for equip in player_data['equip']['5']:
        if equip['status'] == 1:
            now_worn['5'] = equip

    for equip in player_data['equip']['6']:
        if equip['status'] == 1:
            now_worn['6'] = equip
            
    return now_worn

def equip_list_worn(type_list, this_equip):
    # type_list 某类型的装备已全部脱下list列表
    # this_equip 对应装备对象
    this_equip['status'] = 0
    for equip_index in range(len(type_list)):
        if this_equip == type_list[equip_index]:
            # 匹配到
            type_list[equip_index]['status'] = 1
            break
    return type_list

def equip_change_attr_up(self, account, attr_dict):
    # 装备变化后，属性增加
    print(attr_dict)
    new_attr_dict = {
        "ad" : 0,
        "ap" : 0,
        "ad_def" : 0,
        "ap_def" : 0,
    }
    
    for equip in attr_dict:
        equip_id = attr_dict[equip]['id']
        attr1_value = attr_dict[equip]['attr1']
        attr2_value  = attr_dict[equip]['attr2']
        attr1_type = equip_config[str(equip_id)]['attr1'].split("|")[0]
        attr2_type = equip_config[str(equip_id)]['attr2'].split("|")[0]
        new_attr_dict[attr1_type] += attr1_value
        new_attr_dict[attr2_type] += attr2_value

    player_model.player_attribute_dict_change(self, account, new_attr_dict)

def equip_change_attr_reduce(self, account, attr_dict):
    # 装备变化后，属性减少
    # 初始化一堆属性
    print(attr_dict)
    new_attr_dict = {
        "ad" : 0,
        "ap" : 0,
        "ad_def" : 0,
        "ap_def" : 0,
    }
    
    for equip in attr_dict:
        equip_id = attr_dict[equip]['id']
        attr1_value = attr_dict[equip]['attr1']
        attr2_value  = attr_dict[equip]['attr2']
        attr1_type = equip_config[str(equip_id)]['attr1'].split("|")[0]
        attr2_type = equip_config[str(equip_id)]['attr2'].split("|")[0]
        new_attr_dict[attr1_type] -= attr1_value
        new_attr_dict[attr2_type] -= attr2_value

    player_model.player_attribute_dict_change(self, account, new_attr_dict)

def equip_getplayer_equip_attr(self, data_dict):
    data = {}
    account = data_dict['account']
    mycol = mongo_connect()
    check_query = { "account" : account }
    check_doc = mycol.find(check_query)
    check_list = []
    for x in check_doc:
        check_list.append(x)
    player_data = check_list[0]
    del player_data["_id"]
    now_worn = check_now_equip_status(player_data) # 获得当前装备的装备列表
    attr_dict = {
        "ad" : 0,
        "ap" : 0,
        "ad_def" : 0,
        "ap_def" : 0,
    }
    for equip in now_worn:
        equip_id = now_worn[equip]['id']
        attr1_value = now_worn[equip]['attr1']
        attr2_value  = now_worn[equip]['attr2']
        attr1_type = equip_config[str(equip_id)]['attr1'].split("|")[0]
        attr2_type = equip_config[str(equip_id)]['attr2'].split("|")[0]
        attr_dict[attr1_type] += attr1_value
        attr_dict[attr2_type] += attr2_value

    code = "200"
    data["account"] = player_data["account"]
    data["type"] = "equip_worn_attr_get"
    data["code"] = code
    data["equip_attr_data"] = attr_dict
    data = json.dumps(data)
    return data








